#include "Session.h"
#include <iostream>

extern deque<s_info> dq_info;
extern mutex mtx;

Session::Session(boost::asio::io_context& ioc)
    : soc(ioc)
{
}

boost::asio::ip::tcp::socket& Session::get_socket()
{
    return soc;
}

void Session::Start()
{
    memset(data, 0, max_len);
    soc.async_read_some(boost::asio::buffer(data, max_len), std::bind(&Session::handle_recive, this, std::placeholders::_1, std::placeholders::_2));
}

bool Session::check_pos(int pos1, int pos2)
{
    if ((pos1 < 0) || (pos2 < 0))
        return false;
    if (pos1 > pos2)
        return false;
    return true;
}

void Session::handle_recive(const ::boost::system::error_code& error, size_t recived_len)
{
    // 数据格式：192.168.171.88?2024-09-02-19-29-38_0.mp4?0001?02
    if (!error) {
        std::string s_data(data);
        std::string s_data_2 = s_data;
        int pos1 = s_data.find_first_of("?");
        int pos2 = s_data_2.find(".mp4");
        bool res = check_pos(pos1, pos2);
        if (!res)
            return;
        s_info info;
        // std::cout << "pos1="<<pos1<<"pos2="<<pos2<<std::endl;
        // std::cout << s_data<<std::endl;
        info.ip = s_data.substr(0, pos1);
        info.filename = s_data.substr(pos1 + 1, pos2 + 3 - pos1);
        info.sce_name = s_data.substr(pos2 + 5, 4);
        info.pro_name = s_data.substr(pos2 + 5 + 5, 2);
        info.count = 0;
        // std::cout << info.ip<<std::endl;
        // std::cout << info.filename<<std::endl;
        // std::cout <<info.sce_name<<std::endl;
        // std::cout <<info.pro_name<<std::endl;
        mtx.lock();
        int size = dq_info.size();
        if (size > 4) {
            s_info info1 = dq_info.at(size - 1);
            s_info info2 = dq_info.at(size - 2);
            s_info info3 = dq_info.at(size - 3);
            s_info info4 = dq_info.at(size - 4);
            if ((info.filename == info1.filename) || (info.filename == info2.filename) || (info.filename == info3.filename) || (info.filename == info4.filename))
                int a = 1;
            else // 不重复时入队列
            {
                if (info.sce_name == "0007") {
                    dq_info.insert(dq_info.begin(), info);
                    std::cout << "优先入队" << data << std::endl;
                } else
                    dq_info.push_back(info);
                std::cout << "收到的数据是: " << data << std::endl;
            }
        } else if (size == 4) {
            s_info info1 = dq_info.at(size - 1);
            s_info info2 = dq_info.at(size - 2);
            s_info info3 = dq_info.at(size - 3);
            s_info info4 = dq_info.at(size - 4);
            if ((info.filename == info1.filename) || (info.filename == info2.filename) || (info.filename == info3.filename) || (info.filename == info4.filename))
                int a = 1;
            else // 不重复时入队列
            {
                if (info.sce_name == "0007") {
                    dq_info.insert(dq_info.begin(), info);
                    std::cout << "优先入队" << data << std::endl;
                } else
                    dq_info.push_back(info);
                std::cout << "收到的数据是: " << data << std::endl;
            }
        } else if (size == 3) {
            s_info info1 = dq_info.at(size - 1);
            s_info info2 = dq_info.at(size - 2);
            s_info info3 = dq_info.at(size - 3);
            if ((info.filename == info1.filename) || (info.filename == info2.filename) || (info.filename == info3.filename))
                int a = 1;
            else // 不重复时入队列
            {
                if (info.sce_name == "0007") {
                    dq_info.insert(dq_info.begin(), info);
                    std::cout << "优先入队" << data << std::endl;
                } else
                    dq_info.push_back(info);
                std::cout << "收到的数据是: " << data << std::endl;
            }
        } else if (size == 2) {
            s_info info1 = dq_info.at(size - 1);
            s_info info2 = dq_info.at(size - 2);
            if ((info.filename == info1.filename) || (info.filename == info2.filename))
                int a = 1;
            else // 不重复时入队列
            {
                if (info.sce_name == "0007") {
                    dq_info.insert(dq_info.begin(), info);
                    std::cout << "优先入队" << data << std::endl;
                } else
                    dq_info.push_back(info);
                std::cout << "收到的数据是: " << data << std::endl;
            }
        } else if (size == 1) {
            s_info info1 = dq_info.at(size - 1);
            if ((info.filename == info1.filename))
                int a = 1;
            else // 不重复时入队列
            {
                if (info.sce_name == "0007") {
                    dq_info.insert(dq_info.begin(), info);
                    std::cout << "优先入队" << data << std::endl;
                } else
                    dq_info.push_back(info);
                std::cout << "收到的数据是: " << data << std::endl;
            }
        } else {
            dq_info.push_back(info);
            std::cout << "收到的数据是: " << data << std::endl;
        }
        mtx.unlock();
        soc.async_write_some(boost::asio::buffer(data, recived_len), std::bind(&Session::handle_send, this, std::placeholders::_1));
    } else {
        delete this;
    }
}

void Session::handle_send(const ::boost::system::error_code& error)
{
    if (!error) {
        memset(data, 0, max_len);
        soc.async_read_some(boost::asio::buffer(data, max_len), std::bind(&Session::handle_recive, this, std::placeholders::_1, std::placeholders::_2));
    } else {
        delete this;
    }
}
